export class WebGPU {
  initWebGPU = async function (canvas) {
    let gpu = navigator.gpu;
    if (!gpu) {
      throw new Error("Not Support WebGPU");
    }

    let adapter = await gpu.requestAdapter({
      powerPreference: "high-performance",
    });
    if (!adapter) {
      throw new Error("No Adapter Found");
    }

    let device = await adapter.requestDevice();
    let context = canvas.getContext("webgpu");
    let format = gpu.getPreferredCanvasFormat();
    canvas.width = canvas.clientWidth;
    canvas.height = canvas.clientHeight;
    let size = { width: canvas.width, height: canvas.height };
    context.configure({
      device,
      format,
      alphaMode: "opaque",
    });

    return { device, context, format, size };
  };
}
